package com.yuncool.sky.system.web;

import com.yuncool.sky.system.domain.SysParam;
import com.yuncool.sky.system.pojo.SysUser;
import com.yuncool.sky.system.pojo.form.CourseForm;
import com.yuncool.sky.system.pojo.form.SubjectForm;
import com.yuncool.sky.system.pojo.form.UserForm;
import com.yuncool.sky.system.service.CourseClassifyService;
import com.yuncool.sky.system.service.SubjectService;
import com.yuncool.sky.system.service.SysParamService;
import com.yuncool.sky.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther XIE
 * @Date 2019/11/22 22:17
 * @Description 后台主Controller
 * @Version 1.0
 */
@Controller
public class AdminSystemController {

    @Autowired
    SysParamService sysParamService;
    @Autowired
    SubjectService subjectService;
    @Autowired
    CourseClassifyService courseClassifyService;
    @Autowired
    UserService userService;

    @RequestMapping("/")
    public String toRoot() {
        return "login";
    }

    @RequestMapping("login")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("index")
    public String toIndex() {
        return "index";
    }

    /**
     * 后台登陆
     *
     * @param sysUser
     * @return
     */
    @RequestMapping("loginin")
    @ResponseBody
    public String login(@RequestBody SysUser sysUser) {
        SysUser systemUser = sysParamService.getSystemUser();
        if (systemUser.getUsername().equals(sysUser.getUsername()) && systemUser.getPassword().equals(sysUser.getPassword())) {
            return "1";
        }
        return "0";
    }

    /**
     * 系统参数列表
     *
     * @return
     */
    @RequestMapping("sysParamList")
    @ResponseBody
    public Map<String, Object> sysParamList() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "00");
        map.put("data", sysParamService.selectAll());
        return map;
    }

    /**
     * 修改系统参数
     *
     * @param sysParam
     */
    @RequestMapping("editParam")
    @ResponseBody
    public void editParam(SysParam sysParam) {
        sysParamService.updateById(sysParam);
    }

    /**
     * 科目列表
     *
     * @param subjectForm
     * @return
     */
    @RequestMapping("subjectList")
    @ResponseBody
    public Map<String, Object> subjectList(SubjectForm subjectForm) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "00");
        map.put("count", subjectService.sysSubjectSize());
        map.put("data", subjectService.sysSubjectList(subjectForm));
        return map;
    }

    /**
     * 课程分类列表
     *
     * @param courseForm
     * @return
     */
    @RequestMapping("courseList")
    @ResponseBody
    public Map<String, Object> courseList(CourseForm courseForm) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "00");
        map.put("count", courseClassifyService.count());
        map.put("data", courseClassifyService.sysCourseClassifyList(courseForm));
        return map;
    }

    /**
     * 用户列表
     *
     * @param userForm
     * @return
     */
    @RequestMapping("userList")
    @ResponseBody
    public Map<String, Object> userList(UserForm userForm) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "00");
        map.put("count", userService.count());
        map.put("data", userService.sysUserList(userForm));
        return map;
    }
}
